package top.littlejiang.onlinexam.common.configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chen
 * @Description
 * @since 2020/12/25
 */
@Configuration
public class DruidConfig {

    /**
     * 配置druid监控页面
     * @Param:
     * @return: ServletRegistrationBean
     * @author chen
     * @since 2020/12/25 17:00
     */
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//        // 添加IP白名单
//        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
//        // 添加IP黑名单，当白名单和黑名单重复时，黑名单优先级更高
//        servletRegistrationBean.addInitParameter("deny", "192.168.0.19");
//        // 添加控制台管理用户
//        servletRegistrationBean.addInitParameter("loginUsername", "chen");
//        servletRegistrationBean.addInitParameter("loginPassword", "CYH@08287Xa");
//        // 是否能够重置数据
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    /**
     *  配置过滤器
     * @Param:
     * @return: FilterRegistrationBean
     * @author chen
     * @since 2020/12/25 17:06
     */
    @Bean
    public FilterRegistrationBean statFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        // 添加过滤规则
        filterRegistrationBean.addUrlPatterns("/*");
        // 忽略过滤格式
        filterRegistrationBean.addInitParameter("exclusions", "*.gif,*.jpg,*.png,,*.ico,/druid/*,");
        return filterRegistrationBean;
    }

}
